assemble-handle
Assemble pipeline plugin for handling custom middleware stages.
Install
Install with npm:
$ npm install --save assemble-handle
Usage
var handle = require('assemble-handle');
handle
Handle middleware for the given middleware "stage".
app.task('default', function() {
return app.src('*.js')
.pipe(handle(app, 'handlerName'))
.pipe(app.dest('foo'))
});
Example
var assemble = require('assemble');
var handle = require('assemble-handle');
var app = assemble();
app.handler('onStream');
app.handler('preWrite');
app.handler('postWrite');
app.onStream(/\.(js|css)$/, function(file, next) {
next();
});
app.preWrite(/\.(jpg|png)$/, function(file, next) {
next();
});
app.postWrite(/./, function(file, next) {
next();
});
app.task('site', function() {
return app.src('assets/**/*.*')
.pipe(handle(app, 'onStream'))
.pipe(handle(app, 'preWrite'))
.pipe(app.dest('site/'));
.pipe(handle(app, 'postWrite'))
});
handle.once
A .once
method is exposed, which has the same exact behavior as the main function, but will ensure that middleware is only handled once for a given "stage".
Example
For example the given middleware will only run once.
var assemble = require('assemble-core');
var handle = require('assemble-handle');
var app = assemble();
app.handler('onFoo');
app.onFoo(/./, function(file, next) {
file.count = file.count || 0;
file.count++;
next();
});
app.task('handle-once', function(cb) {
var files = [];
app.src('test/**/*.*')
.pipe(handle.once(app, 'onFoo'))
.pipe(handle.once(app, 'onFoo'))
.pipe(handle.once(app, 'onFoo'))
.pipe(handle.once(app, 'onFoo'))
.pipe(handle.once(app, 'onFoo'))
.on('data', function(file) {
files.push(file);
})
.pipe(app.dest('test/actual'))
.on('end', function() {
console.log(files[0].count);
cb();
});
});
app.task('handle', function(cb) {
var files = [];
app.src('test/**/*.*')
.pipe(handle(app, 'onFoo'))
.pipe(handle(app, 'onFoo'))
.pipe(handle(app, 'onFoo'))
.pipe(handle(app, 'onFoo'))
.pipe(handle(app, 'onFoo'))
.on('data', function(file) {
files.push(file);
})
.pipe(app.dest('test/actual'))
.on('end', function() {
console.log(files[0].count);
cb();
});
});
Custom handlers
Create custom middleware handlers.
app.handler('onFoo');
This adds an .onFoo
method to the instance:
app.onFoo(/\.hbs$/, function(file, next) {
next();
});
All .onFoo
middleware will now run when the onFoo
handler is called:
app.task('default', function() {
return app.src('*.hbs')
.pipe(handle(app, 'onFoo'))
});
About
Related projects
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on May 28, 2017.